<template>
  <div class="app-container">
    <Search @reset="reset" @search="handleSearch">
      <div>
        <label>用户名称</label>
        <el-input v-model="searchParams.user_name" placeholder="请输入" clearable @change="handleSearch"></el-input>
      </div>

      <div>
        <label>用户邮箱</label>
        <el-input v-model="searchParams.user_email" placeholder="请输入" clearable @change="handleSearch"></el-input>
      </div>
    </Search>
    <vxe-grid v-bind="gridConfig" class="sticky-header">
      <template #toolbar_buttons>
        <!-- <el-button icon="PlusIcon" @click="operateRef.open({ type: 'add' })">新增</el-button> -->
      </template>

      <template #operate="{ row }">
        <el-button link @click="operateRef.open({ type: 'detail', contact_id: row.contact_id })">详情</el-button>
        <el-popconfirm title="确定删除吗?" @confirm="handleDelete(row.contact_id)">
          <template #reference>
            <el-button link type="danger">删除</el-button>
          </template>
        </el-popconfirm>
      </template>
      <template #pager>
        <vxe-pager
          v-model:current-page="searchParams.pageNum"
          v-model:page-size="searchParams.pageSize"
          :total="searchParams.total"
          @page-change="getList"
        >
        </vxe-pager>
      </template>
    </vxe-grid>
  </div>

  <operate-component ref="operateRef" :options="options" @refresh-list="getList"></operate-component>
</template>

<script lang="ts" setup>
import Search from "@/components/Search/index.vue"
import { reactive, onMounted, ref } from "vue"
import { VxeGridProps } from "vxe-table"
import { ElMessage } from "element-plus"
import { getContactList, contactDelete } from "@/api/contactManage/index"
import { getAllUser } from "@/api/userManage/index"
import operateComponent from "./operate.vue"

defineOptions({
  name: "Contact"
})

let previewSrcList = ref([])
const operateRef = ref()
const searchParams = reactive(setDefaultSearchParams())
const options = reactive({})
const gridConfig = reactive<VxeGridProps>({
  id: "Contact", // 表格唯一标识
  customConfig: {
    storage: true
  },
  toolbarConfig: {
    custom: true,
    slots: {
      buttons: "toolbar_buttons"
    }
  },
  columns: [
    { type: "seq", width: 50 },
    { title: "留言id", field: "contact_id" },
    { title: "用户名称", field: "user_name" },
    { title: "用户邮箱", field: "user_email" },
    { title: "留言问题", field: "contact_query" },
    { title: "留言内容", field: "contact_message" },
    { title: "日期", field: "createdAt" },
    { title: "操作", field: "operate", slots: { default: "operate" }, showOverflow: false, width: 180 }
  ],
  data: []
})

function handleDelete(contact_id: number) {
  contactDelete({ contact_id }).then(() => {
    ElMessage.success("删除成功")
    getList()
  })
}

function handleSearch() {
  searchParams.pageNum = 1
  getList()
}

function reset() {
  Object.assign(searchParams, setDefaultSearchParams())
  handleSearch()
}

function setDefaultSearchParams() {
  return {
    user_email: "",
    user_name: "",
    pageNum: 1,
    pageSize: 10,
    total: 0
  }
}

function getList() {
  getContactList(searchParams).then((res: any) => {
    gridConfig.data = res.data.list
    searchParams.total = res.data.total
    // previewSrcList = res.data.list.map((item: { contact_path: any }) => item.contact_path)
  })
}

onMounted(() => {
  getList()
})
</script>

<style lang="scss" scoped></style>
